//
//  HTAnime.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/10.
//

import Foundation
import HandyJSON

struct HTAnime: HandyJSON {
    var var_id: String = ""
    var var_banner_animeId: String = ""// banner下剧集id
    var var_title: String?// 剧名
    var var_title_2: String?
    var var_description: String?// 描述
    var var_k1: String?
    var var_cover: String?// 封面
    var var_genres: String?// 类型
    var var_age: String?// 年龄分级,1代表18+，其他为非18+
    var var_rate: String?// 评分
    var var_views: String?// 浏览量
    var var_pub_date: String?// 发布时间
    var var_z_type: String?
    var var_startTime: String?
    var var_quality: String?// 清晰度，CAM、HD等
    var var_ep: String?// 集数 Ep Full代表Movie(电影不显示集按钮)，其他文案表示集信息
    var var_cur_ep: String?
    var var_cur_epId: String?
    var var_progressRate: Double?// 历史播放的进度
    var var_during: Double?// 历史播放时间点
    var var_new_flag: String?// 是否有更新: 1是，0否
    var var_finished: String?// 是否完结：1是，0否
    var var_sub: String?// 线路SUB是否存在:1存在，0不存在
    var var_dub: String?// 线路DUB是否存在:1存在，0不存在
    var var_a20: [HTAnime]?
    var var_eps_list: [HTEp]?
    var var_eps_dict: [String: [HTEp]]?
    var var_ssn_list: [String]?// 分集
    var var_play_lock: String?
    
    mutating func mapping(mapper: HelpingMapper) {
        mapper <<<
            self.var_id <-- "id"
        
        mapper <<<
            self.var_banner_animeId <-- "new_conf_value"
        
        mapper <<<
            self.var_title <-- ["title", "new_conf_name", "name"]
        
        mapper <<<
            self.var_title_2 <-- "title_2"
        
        mapper <<<
            self.var_description <-- "description"
        
        mapper <<<
            self.var_k1 <-- "k1"
        
        mapper <<<
            self.var_cover <-- ["cover", "img"]
        
        mapper <<<
            self.var_genres <-- "genres"
        
        mapper <<<
            self.var_age <-- "age"
        
        mapper <<<
            self.var_rate <-- "rate"
        
        mapper <<<
            self.var_views <-- "views"
        
        mapper <<<
            self.var_pub_date <-- "pub_date"
        
        mapper <<<
            self.var_z_type <-- "z_type"
        
        mapper <<<
            self.var_quality <-- "quality"
        
        mapper <<<
            self.var_ep <-- "ep"
        
        mapper <<<
            self.var_cur_ep <-- "cur_ep"
        
        mapper <<<
            self.var_cur_epId <-- "cur_epId"
        
        mapper <<<
            self.var_progressRate <-- "progressRate"
        
        mapper <<<
            self.var_during <-- "during"
        
        mapper <<<
            self.var_startTime <-- "startTime"
        
        mapper <<<
            self.var_new_flag <-- "new_flag"
        
        mapper <<<
            self.var_finished <-- "finished"
        
        mapper <<<
            self.var_sub <-- "sub"
        
        mapper <<<
            self.var_dub <-- "dub"
        
        mapper <<<
            self.var_a20 <-- "a20"
        
        mapper <<<
            self.var_eps_list <-- "eps_list"
        
        mapper <<<
            self.var_eps_dict <-- "eps_dict"
        
        mapper <<<
            self.var_ssn_list <-- "ssn_list"
        
        mapper <<<
            self.var_play_lock <-- "play_lock"
    }
    
    func ht_subTitle () -> String {
        var var_subTitle = ""
        if (self.var_dub == "1") {
            var_subTitle += " · DUB"
        }
        if (self.var_finished == "1") {
            var_subTitle += " · Airing"
        } else {
            var_subTitle += " · Finshed"
        }
        guard let var_rang = Range(NSRange(location: 0, length: 3), in: var_subTitle) else { return "" }
        return var_subTitle.replacingCharacters(in: var_rang, with: "")
    }
    
    func ht_searchTag () -> String {
        var var_tag = " · SERIES"
        if (self.var_dub == "1") {
            var_tag += " · DUB"
        }
        if (self.var_sub == "1") {
            var_tag += " · SUB"
        }
        guard let var_rang = Range(NSRange(location: 0, length: 3), in: var_tag) else { return "" }
        return var_tag.replacingCharacters(in: var_rang, with: "")
    }
    
    func ht_searchEps () -> [HTEp?] {
        if (self.var_eps_list!.count <= 6) {
            return self.var_eps_list!
        } else {
            return [self.var_eps_list![0], self.var_eps_list![1], nil, self.var_eps_list![self.var_eps_list!.count - 3], self.var_eps_list![self.var_eps_list!.count - 2], self.var_eps_list![self.var_eps_list!.count - 1]]
        }
    }
    
    func ht_progressStr() -> String {
        if (self.var_progressRate != nil && !self.var_progressRate!.isInfinite) {
            return "\(Int(round((self.var_progressRate ?? 0) * 100)))%"
        } else {
            return "0%"
        }
    }
    
    func ht_allEps() -> [HTEp] {
        guard ((self.var_ssn_list) != nil && (self.var_eps_dict) != nil) else {
            return [] as [HTEp]
        }
        var var_fullEps:[HTEp] = []
        for i in 0..<self.var_ssn_list!.count {
            let var_stag = self.var_ssn_list![i]
            var_fullEps += self.var_eps_dict![var_stag]!
        }
        return var_fullEps
    }
    
    func ht_curEp(ep_id: String = "") -> HTEp {
        let var_allEps = self.ht_allEps()
        if (ep_id == "") {
            let var_lastEps = self.var_eps_dict![self.var_ssn_list![self.var_ssn_list!.count - 1]]!
            let var_matchEp = var_lastEps.first
            return var_matchEp ?? HTEp()
        } else {
            let var_matchEp = var_allEps.filter {$0.var_id == ep_id}.first
            return var_matchEp ?? HTEp()
        }
    }
    
    func ht_currentSsns(ep_id: String = "") -> [HTEp] {
        var var_currentSsns: [HTEp] = []
        let var_curEp = self.ht_curEp(ep_id: ep_id)
        for var_item in (self.var_eps_dict ?? [:]) {
            if (((var_curEp.var_eps_num ?? "").ht_inRange(var_rawString: var_item.key)) == true) {
                var_currentSsns = var_item.value
                break
            }
        }
        
        return var_currentSsns
    }
    
    func ht_currentSsnTitle(ep_id: String = "") -> String {
        var var_currentSsnTitle: String = ""
        let var_curEp = self.ht_curEp(ep_id: ep_id)
        for var_item in (self.var_eps_dict ?? [:]) {
            if (((var_curEp.var_eps_num ?? "").ht_inRange(var_rawString: var_item.key)) == true) {
                var_currentSsnTitle = var_item.key
                break
            }
        }
        
        return var_currentSsnTitle
    }
}
